
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.swing.*;

public class DrawingFrame extends JPanel implements MouseListener, MouseMotionListener{
    
    private boolean editable;
    private ArrayList<Line> lines;
    private ArrayList<Point> intersections;
    private int x1, x2, y1, y2;
    private int x_offset, y_offset;
    private boolean dragging, right_click;
    private double zoom_level;
    private Point2D point;
    private MainFrame mainFrame;
    private JScrollPane scroll;
    private JScrollBar horizontal, vertical;
    
    public DrawingFrame(MainFrame frame){
        super();
        mainFrame = frame;
        editable = false;
        zoom_level = 1;
        dragging = false;
        right_click = false;
        lines = new ArrayList<Line>();
        intersections = new ArrayList<Point>();
        setBackground(Color.white);
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    
    public void setEnable(boolean status){
        editable = status;
    }
    
    public void setZoom(double zoom_value){
        double old_zoom_level = zoom_level;
        zoom_level = zoom_value;
        double factor = zoom_level / old_zoom_level;
        double width = factor * getWidth();
        double height = factor * getHeight();
        setPreferredSize(new Dimension((int) width, (int) height));
        repaint();
        revalidate();
    }
    
    public void setLines(ArrayList<Line> new_lines){
        lines = new_lines;
        mainFrame.setNumberOfSegments(lines.size());
        repaint();
    }
    
    public ArrayList<Line> getLines(){
        return lines;
    }
    
    public void setIntersections(ArrayList<Point> points){
        intersections = points;
        repaint();
    }
    
    public ArrayList<Point> getIntersections(){
        return intersections;
    }
    
    public void undo(){
        if(lines.size() != 0){
            lines.remove(lines.size() -1);
            repaint();
            mainFrame.setNumberOfSegments(lines.size());
        }
    }

    public void mouseClicked(MouseEvent me) {
        //Do nothing
    }

    public void mousePressed(MouseEvent me) {
        if(me.getButton() == me.BUTTON3) right_click = true;
        else{
            x1 = me.getX();
            y1 = me.getY();
        }
        dragging = true;
    }

    public void mouseReleased(MouseEvent me) {
        if(!right_click){
            System.out.println("Paint dragged segment");
            dragging = false;
            x2 = me.getX();
            y2 = me.getY();
            if(x1 != x2 || y1 != y2){
                Point p1 = new Point(x1 / zoom_level, (getHeight() - y1) / zoom_level);
                Point p2 = new Point(x2 / zoom_level, (getHeight() - y2) / zoom_level);
                lines.add(new Line(p1,p2));
                mainFrame.setNumberOfSegments(lines.size());
            }
        } else {
            right_click = false;
        }
        repaint();
    }

    public void mouseEntered(MouseEvent me) {
        //Do nothing
    }

    public void mouseExited(MouseEvent me) {
        //Do nothing
    }
    
    public void paintComponent(Graphics g){
        System.out.println(x_offset);
        System.out.println(y_offset);
        Graphics2D g2 = (Graphics2D) g;
        g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ));
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
        g2.scale(zoom_level, zoom_level);
        super.paintComponent(g2);
        for(Line2D line: lines){
            int x1 = (int) (line.getX1()) + x_offset;
            int x2 = (int) (line.getX2()) + x_offset;
            int y1 = (int) (getHeight()/zoom_level - line.getY1()) + y_offset;
            int y2 = (int) (getHeight()/zoom_level - line.getY2()) + y_offset;
            g2.drawLine(x1, y1, x2, y2);
        }
        // faudra peut etre faire quelque chose pour les intersections et le zoom
        g2.setColor(Color.red);
        for(Point2D point: intersections){
            int x = (int) point.getX() -1;
            int y = (int) (getHeight()/zoom_level - point.getY()) -1;
            g2.drawOval(x, y, 1, 1);
        }
        g2.setColor(Color.blue);
        if(!right_click && dragging){
            int xp1 = (int) (x1 / zoom_level);
            int yp1 = (int) (y1 / zoom_level);
            int xp2 = (int) (point.getX() / zoom_level);
            int yp2 = (int) (point.getY() / zoom_level);
            g2.drawLine(xp1, yp1, xp2, yp2);
        }
    }

    public void mouseDragged(MouseEvent me) {
        if(dragging){
            point = me.getPoint();
            System.out.println("dragging");
            if(right_click){
                x_offset = (int) point.getX();
                y_offset = (int) point.getY();
            }
            /*int width = (int) (getWidth() / zoom_level);
            int height = (int) (getHeight() / zoom_level);
            if(point.getX() > width && point.getY() > height){
                height = (int) point.getY();
                width = (int) point.getX();
                mainFrame.moveScrollBars(new java.awt.Point((int)point.getX(), (int)point.getY()));
            }
            else if(point.getX() > width){
                width = (int) point.getX();
                mainFrame.moveScrollBars(new java.awt.Point((int)point.getX(), (int)point.getY()));
            }
            else if(point.getY() > height){
                height = (int) point.getY();
                mainFrame.moveScrollBars(new java.awt.Point((int)point.getX(), (int)point.getY()));
            }
            setPreferredSize(new Dimension(width, height));*/
            revalidate();
            repaint(); // apparition de bugs quand zoom > 1
        }
    }

    public void mouseMoved(MouseEvent me) {
        //Do nothing
    }
     
}
